//
//  AddFriendsViewController.m
//  一点互助
//
//  Created by rimi on 16/9/6.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "AddFriendsViewController.h"
#import "ResponseManager.h"
#import "FBProgressView.h"
#import "UIViewController+NavagitionBar.h"

@interface AddFriendsViewController ()

@end

@implementation AddFriendsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:0.8478 green:0.8478 blue:0.8478 alpha:1.0]];
    self.nickNameLabel.text = self.nickName;
    PLLOG(@"%@",self.nickNameLabel.text);
    PLLOG(@"%@",self.idStr);
    PLLOG(@"%ld",(long)self.mobile);
    if ([self.nickName isEqualToString:@"NULL"]) {
        [self.btn setEnabled:NO];
        [self.btn setBackgroundColor:[UIColor grayColor]];
    }else{
        if (self.mobile == 0) {
            [self getSearch];
        }
    }

}
-(void)getSearch{
    NSDictionary *dic = @{@"search":self.idStr};
    [ResponseManager startPostRequestWithURLString:kSearchUserURL parameters:dic completionHandler:^(FBResponse *response) {
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
            self.mobile = [response.resultValue[@"mobile"]integerValue];
        }else{
            [self.btn setEnabled:NO];
            [self.btn setBackgroundColor:[UIColor grayColor]];
        }
    }];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
  
}
-(void)viewWillLayoutSubviews{
    self.btn.layer.cornerRadius = self.btn.bounds.size.height/4;
}
- (IBAction)addFriendsBtnTaped:(id)sender {
    __weak typeof(self) myself = self;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [defaults objectForKey:@"userInfo"];
    NSString *idStr = dic[@"id"];
    [FBProgressView showWithMessage:nil];
//    [[EMClient sharedClient].contactManager asyncAddContact:[NSString stringWithFormat:@"%ld",(long)self.mobile] message:@"111" success:^{
//        [FBProgressView hide];
//        [myself addAlertControllerWithTitle:@"提示" message:@"已发送" actionNum:1 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
//            [myself.navigationController popViewControllerAnimated:YES];
//        }];
//    } failure:^(EMError *aError) {
//        [FBProgressView hide];
//        [myself addAlertControllerWithTitle:@"提示" message:aError.description actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
//            [myself addFriendsBtnTaped:myself.btn];
//        }];
//    }];

    NSDictionary *parameters = @{@"friend_id":self.idStr,@"user_id":idStr};
    [ResponseManager startPostRequestWithURLString:kAddFriendsURL parameters:parameters completionHandler:^(FBResponse *response) {
        if (response.success) {
            [FBProgressView hide];
            PLLOG(@"%@,%@",response.resultValue,response.resultDesc);
            [[EMClient sharedClient].contactManager asyncAddContact:self.idStr message:@"111" success:^{
                [myself addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:1 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                    [myself.navigationController popViewControllerAnimated:YES];
                }];
            } failure:^(EMError *aError) {
                [myself addAlertControllerWithTitle:@"提示" message:aError.errorDescription actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                    [myself addFriendsBtnTaped:myself.btn];
                }];
            }];
        }else{
            [FBProgressView hide];
            PLLOG(@"%@",response.resultDesc);
            [myself addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                [myself addFriendsBtnTaped:myself];
            }];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
